from network_form.utils_network import read_data,load_all_graphs,form_feature,weight_edge,count_topics
import numpy as np

data_path = "../data_process/All_Unpack/"
graphs_path = "../network_form/all_graphs.pkl"


# 参与者或者缺席者与event相似度分布
if __name__ == "__main__":
    event_list, topic_dict, member_list, group_list = read_data(data_path, rebuild=False)
    # all_graphs = load_all_graphs(graphs_path)
    group_topics={}
    for item in group_list:
        group_topics[item['id']]=item['topics']

    event_topics={}
    for item in event_list:
        group_id=item['group_id']
        event_topics[item['id']]=group_topics[group_id]

    member_topics={}
    for item in member_list:
        member_topics[item['id']]=item['topics']
        if item['id']=='10393986':
            print('id')

    print('begin')
    len_x=10
    width_x=1/len_x
    sum_cos=np.zeros((len_x,))
    cnt=0
    for event in event_list:
        event_id=event['id']
        group_id=event['group_id']
        member_response=event['member and response']
        # member_ids=list(event['member and response'].keys())
        if len(member_response)<=0:
            continue

        event_topic=event_topics[event_id]
        event_i=np.zeros((len_x,))
        member_cnt=0
        for member_id,response in member_response.items():
            if member_id not in member_topics:
                continue

            if response!='yes':
                continue
            member_cnt+=1
            member_topic=member_topics[member_id]
            new_member_topic=member_topic.copy()
            new_member_topic.extend(event_topic)
            event_topics_dict=list(set(new_member_topic))
            feature_dim=len(event_topics_dict)

            member_feature=form_feature(member_topic, feature_dim, event_topics_dict)
            event_feature=form_feature(event_topic, feature_dim, event_topics_dict)
            sim=weight_edge(member_feature,event_feature)
            i=int(sim/width_x)
            if i>=len_x:
                i=len_x-1
            event_i[i]+=1

        if member_cnt==0:
            continue

        # event_i=event_i/member_cnt
        sum_cos+=event_i
        cnt+=member_cnt
        # print(event_i)
    sum_cos=sum_cos/cnt
    print(list(sum_cos))
    print(np.sum(sum_cos))







